<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>为一幅图像分配颜色</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.imagecolorallocate" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">imagecolorallocate</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">imagecolorallocate</span> &mdash; <span class="dc-title">为一幅图像分配颜色</span></p>

 </div>
 <div class="refsect1 unknown-unknown-unknown-examplev" id="refsect1-function.imagecolorallocate-unknown-unknown-unknown-examplev">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>imagecolorallocate</strong></span>
    ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$image</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$red</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$green</code></span>
   , <span class="methodparam"><span class="type">int</span> <code class="parameter">$blue</code></span>
   ) : <span class="type">int</span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>imagecolorallocate()</strong></span> 返回一个标识符，代表了由给定的
   <acronym title="Red-Green-Blue">RGB</acronym> 成分组成的颜色。<code class="parameter">red</code>，<code class="parameter">green</code> 和
   <code class="parameter">blue</code> 分别是所需要的颜色的红，绿，蓝成分。这些参数是
   0 到 255 的整数或者十六进制的  0x00 到 0xFF。<span class="function"><strong>imagecolorallocate()</strong></span>
   必须被调用以创建每一种用在 <code class="parameter">image</code> 所代表的图像中的颜色。
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    第一次对 <span class="function"><strong>imagecolorallocate()</strong></span>
    的调用会给基于调色板的图像填充背景色，即用 <span class="function"><a href="imagecreate.html" class="function">imagecreate()</a></span> 建立的图像。
   </p>
  </p></blockquote>
  <p class="para">
   <div class="example" id="example-3661">
    <p><strong>Example #1 <span class="function"><strong>imagecolorallocate()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$im&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">imagecreate</span><span style="color: #007700">(</span><span style="color: #DD0000">'example.jpg'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">100</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">100</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;背景设为红色<br /></span><span style="color: #0000BB">$background&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">imagecolorallocate</span><span style="color: #007700">(</span><span style="color: #0000BB">$im</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">255</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;设定一些颜色<br /></span><span style="color: #0000BB">$white&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">imagecolorallocate</span><span style="color: #007700">(</span><span style="color: #0000BB">$im</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">255</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">255</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">255</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$black&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">imagecolorallocate</span><span style="color: #007700">(</span><span style="color: #0000BB">$im</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;十六进制方式<br /></span><span style="color: #0000BB">$white&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">imagecolorallocate</span><span style="color: #007700">(</span><span style="color: #0000BB">$im</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0xFF</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0xFF</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0xFF</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$black&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">imagecolorallocate</span><span style="color: #007700">(</span><span style="color: #0000BB">$im</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0x00</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0x00</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0x00</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
  <p class="para">
   如果分配失败则返回 -1。
  </p>
  <p class="para">
   参见 <span class="function"><a href="imagecolorallocatealpha.html" class="function">imagecolorallocatealpha()</a></span> 和
   <span class="function"><a href="imagecolordeallocate.html" class="function">imagecolordeallocate()</a></span>。
  </p>
 </div>

</div></div></div></body></html>